//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "ia\fn_hide.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_object","_position","_radius","_bldgpos","_bldgs","_i","_bldarr","_pos"];
DEFAULT_PARAM(_object,0,objNull);
DEFAULT_PARAM(_radius,1,60);

_position = [0,0,0];
if (IS_OBJECT(_object) && {!isNull _object}) then {
	// We send units (if any are left) to building pos if any
	_bldgpos = [];
	_bldgs = (getPos _object) nearObjects ["Building", _radius];
	If (count _bldgs > 0) then {
		for "_i" from 0 to (count _bldgs - 1) do {
			// We only take the first position of each building.
			_bldarr = [];
			// Using homebrewn building pos
			_bldarr = [(_bldgs select _i),TMF_BUILDING_POS_HIDE,True, True] call FFUNC(buildingPosArray);
			DEBUG_1("Building Array",ARR_3(_object,typeof (_bldgs select _i),_bldarr));
			If (count _bldarr > 0) then {
				_bldgpos = [_bldgpos, _bldarr] call FFUNC(arrayPushStack);
			};
		};
		DEBUG_1("BuildingPos to assign",ARR_2(_object, count _bldgpos));
		if (count _bldgpos > 0) then {
			_position = _bldgpos call FFUNC(arrayRandomVal);
			_pos = (_position select 0);
			_pos set [2, (_pos select 2) + 0.3];
			_object setPosATL _pos;
			_object setDir (_position select 1);
			DEBUG_1("Position found for assignment",ARR_3(_object, _position, count(_bldgpos)));
		};
	};
} else {
	TRACE_1("Invalid Object",_object);
};

TRACE_1("End",_position);
TMFPROFILERSTOP;
_position